package homework.module2;

import java.util.Scanner;

public class Wuziqi {

    String[] row = {" ","0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"};
    String[][] board = new String[row.length][row.length];  // 因为第一个字符是""


    /**
     * 绘制棋盘
     */
    public void drawBoard(){
        for(int i=0;i<row.length;i++){
            for(int j=0;j<row.length;j++){
                board[i][j] = "+";
                if (i == 0) {
                    board[i][j] = row[j];
                }
                if(j==0){
                    board[i][j] = row[i];
                }
                if (i==0 && j==0){
                    System.out.print(board[i][j]);
                }else{
                    System.out.print(board[i][j] + " ");
                }

            }
//            System.out.println("\n");
            System.out.println();
        }
    }


    public void printBoard(){
        for(int i=0;i<row.length;i++){
            for(int j=0;j<row.length;j++){
                if (i==0 && j==0){
                    System.out.print(board[i][j]);
                }else{
                    System.out.print(board[i][j] + " ");
//                    System.out.print(board[i][j]);
                }
            }
            System.out.println();
//            System.out.println("\n");
        }
    }


    public void playGame(){
        boolean player = true;   // true-白子，false-黑子

         // 不结束的话一直持续
         while(true){
             if (player){
                 System.out.println("请白方下棋，输入横坐标和纵坐标");
             }else{
                 System.out.println("请黑方下棋，输入横坐标和纵坐标");
             }

             // 输入格式"15,2"
             Scanner scanner = new Scanner(System.in);
             String line = scanner.nextLine();
             String[] lineArray = line.trim().split(",");

             int rowNum = Integer.valueOf(lineArray[0])+1;
             int colNum = Integer.valueOf(lineArray[1])+1;

             if (rowNum>row.length-1 || colNum>row.length-1){
                 System.out.println("输入的横纵坐标不符合规定，请重新输入");
                 continue;
             }

             if (board[rowNum][colNum]!="+"){
                 System.out.println("位置已有棋子，请重新输入");
                 continue;
             }


             board[rowNum][colNum] = player==true?"〇":"●";
             player = player==false?true:false;

             boolean result = this.isWin();
             if (result){
                 String winner = player==false?"白棋":"黑棋";
                 System.out.println("你赢啦你赢啦你赢啦，胜者:" + winner);
                 break;
             }
             this.printBoard();
        }
    }


    public boolean isWin(){
        for(int i=2;i<row.length-2;i++){
            for(int j=2;j<row.length-2;j++){
                String piece = board[i][j];

                if (piece=="+")
                    continue;

                // 横向判断
                if (board[i][j-2].equals(board[i][j-1]) && board[i][j-1].equals(piece) &&
                        piece.equals(board[i][j+1]) && board[i][j+1].equals(board[i][j+2])){
                    return true;
                }

                // 纵向判断
                if (board[i-2][j].equals(board[i-1][j]) && board[i-1][j].equals(piece) &&
                        piece.equals(board[i+1][j]) && board[i+1][j].equals(board[i][j+2])){
                    return true;
                }

                // 左斜向判断
                if (board[i-2][j-2].equals(board[i-1][j-1]) && board[i-1][j-1].equals(piece) &&
                        piece.equals(board[i+1][j+1]) && board[i+1][j+1].equals(board[i+2][j+2])){
                    return true;
                }

                // 右斜向判断
                if (board[i-2][j+2].equals(board[i-1][j+1]) && board[i-1][j+1].equals(piece) &&
                        piece.equals(board[i+1][j-1]) && board[i+1][j-1].equals(board[i+2][j-2])){
                    return true;
                }
            }
        }
        return false;
    }


    public static void main(String[] args) {
        Wuziqi wuziqi = new Wuziqi();
        wuziqi.drawBoard();

        wuziqi.printBoard();
        wuziqi.playGame();

    }

}
